"use strict"
const moment = require('moment')
module.exports = app => {
    const { STRING, DATE, INTEGER } = app.Sequelize
    const Category = app.model.define('category', {
        id: {
            type: INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        name: {
            type: STRING,
            allowNull: false
        },
        rank: {
            type: STRING,
            allowNull: false
        },
        img: {
            type: STRING,
            allowNull: false
        },
        exhibition_img: {
            type: STRING,
            allowNull: false
        },
        pid: {
            type: INTEGER,
            allowNull: false
        },
        create_time: {
            type: DATE,
            allowNull: false,
            get() {
                return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss')
            }
        },
        update_time: {
            type: DATE,
            allowNull: false,
            get() {
                return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss')
            }
        },
    }) 
    Category.associate = function() {
        // 查询子分类
        Category.hasMany(Category, {as: 'children', foreignKey: 'pid', targetKey: 'id'});
        // 查询父分类
        Category.belongsTo(Category, {as: 'parent', foreignKey: 'pid', targetKey: 'id'});
    }
    return Category
}